﻿namespace LogManager.Core.Models
{
    using LogManager.Core.Helpers;
    using LogManager.Utils.Unity;
    using LogManager.Utils.Xml;
    using System.Collections.ObjectModel;
    using System.Collections.Specialized;
    using System.Linq;
    using System.Xml.Linq;
    using LogManager.Core.Factories;


    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class Warehouse : XmlElementBase, IWarehouse
    {
        public const string DEFAULT_TEMPLATE = @"<Warehouse Name=""Warehouse name"" Ip=""Network IP or Name""  Domain=""domain"" Login=""login"" Password=""passWord"">" + Plateform.DEFAULT_TEMPLATE + "</Warehouse>";

        public virtual string Name { get; set; }
        public virtual string Domain { get; set; }
        public virtual string Login { get; set; }
        public virtual string Password { get; set; }
        public virtual string Ip { get; set; }
        public IConfiguration ParentConfiguration { get; protected set; }
        public ObservableCollection<IPlateform> Plateforms { get; protected set; }

        public readonly IXmlElementFactory _xmlElementFactory;

        public Warehouse(XElement baseNode, IConfiguration parentConfiguration, IXmlElementFactory xmlElementFactory)
            : base(baseNode)
        {
            this._xmlElementFactory = xmlElementFactory;
            this.ParentConfiguration = parentConfiguration;
            this.Initialize();
            this.PropertyChanged += (a, b) => SaveChanges();
            this.Plateforms.CollectionChanged += (a, b) => SaveChanges();
        }

        private void Initialize()
        {
            var xml = new XmlResolver<IWarehouse>(BaseNode);
            this.Name = xml.GetAttributeValue(c => c.Name);
            this.Ip = xml.GetAttributeValue(c => c.Ip);
            this.Domain = xml.GetAttributeValue(c => c.Domain);
            this.Login = xml.GetAttributeValue(c => c.Login);
            this.Password = xml.GetAttributeValue(c => c.Password);
            this.Plateforms = new ObservableCollection<IPlateform>(xml.ResolveChildren(c => c.Plateforms, xElm => this._xmlElementFactory.CreatePlateform(xElm, this)));
        }

        public override void SaveChanges()
        {
            var xml = new XmlResolver<IWarehouse>(BaseNode);
            xml.SetAttributeValue(c => c.Name, Name);
            xml.SetAttributeValue(c => c.Domain, Domain);
            xml.SetAttributeValue(c => c.Ip, Ip);
            xml.SetAttributeValue(c => c.Login, Login);
            xml.SetAttributeValue(c => c.Password, Password);
            xml.SetChildren(c => c.Plateforms, Plateforms.OfType<IXmlElement>());
        }

        public void DeleteWarehouse()
        {
            this.ParentConfiguration.Warehouses.Remove(this);
        }

        public IPlateform AddNewPlateform()
        {
            var newPlateform = this._xmlElementFactory.CreateNewPlateform(this);
            this.Plateforms.Add(newPlateform);
            return newPlateform;
        }

    }
}
